<?php
/**
 * Created by PhpStorm.
 * User: renshan
 * Date: 16-6-18
 * Time: 下午4:04
 */

namespace ApiBundle\Controller;

use ApiBundle\Response\SecurityResponse;
use ApiBundle\Security\AppTokenInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use UserBundle\Security\UserTokenInterface;

/**
 * Class BillController
 * @package ApiBundle\Controller
 * 获取账单类型、账单分类。
 * 添加账单分类、修改账单分类。
 * 添加账单、修改账单、删除账单。
 * 返回数据统一由SecurityResponse封装
 */
class BillController extends Controller implements AppTokenInterface, UserTokenInterface
{
    private $security_response;
    private $response;

    /**
     * BillController constructor.
     * 初始化返回类型
     */
    public function __construct()
    {
        $this->security_response    = new SecurityResponse();
        $this->response             = new Response();
        $this->response->headers->set('Content-Type', 'application/json');
    }

    /**
     * @return Response
     * 获取账单类型。
     * 如果服务端已存在类型数据，则返回对相应数据和TYPE_RETURN_OK响应。
     * 否则返回空数组和TYPE_RETURN_ZERO响应。
     */
    public function getTypeAction()
    {
        $data = [];
        $types = $this->get('api.user')->getTypes();

        if($types == null) {
            $this->security_response->setStatus(SecurityResponse::TYPE_RETURN_ZERO);
            $this->security_response->setMessage(SecurityResponse::TYPE_RETURN_ZERO_MESSAGE);
        } else {
            foreach ($types as $type) {
                $data[$type->getId()]['id']   = $type->getId();
                $data[$type->getId()]['name'] = $type->getName();
            }

            $this->security_response->setStatus(SecurityResponse::TYPE_RETURN_OK);
            $this->security_response->setMessage(SecurityResponse::TYPE_RETURN_OK_MESSAGE);
        }

        $this->security_response->setData($data);
        return $this->response->setContent($this->security_response);

    }

    /**
     * @return Response
     * 获取用户的账单分类。
     * 如果存在分类，则返回分类数据和CATEGORY_RETURN_OK响应。
     * 否则返回空数组和CATEGORY_RETURN_ZERO响应。
     */
    public function getCategoryAction()
    {
        //返回数据
        $data       = [];

        //通过service获取用户api_token，已经验证过
        //具体实现可看ApiBundle\Service\ApiTokenService
        //$api_token  = $this->get('api.token');

        $user_service = $this->get('api.user');

        /**
         * 通过api_token获取用户信息，实际上并没有再次读取数据库，
         * 因为UserBundle\Repository\UserRepository中做了单次访问缓存.
         * 实现过程可看ApiBundle\Service\UserService
         */
        $user       = $user_service->getUser();
        
        //通过用户查询所拥有的分类
        $categories = $user_service->getCategory();


        if($categories == null) {
            $this->security_response->setStatus(SecurityResponse::CATEGORY_RETURN_ZERO);
            $this->security_response->setMessage(SecurityResponse::CATEGORY_RETURN_ZERO_MESSAGE);
        } else {
            foreach ($categories as $category) {
                $data[$category->getId()]['id']     = $category->getId();
                $data[$category->getId()]['name']   = $category->getName();
                $this->security_response->setStatus(SecurityResponse::CATEGORY_RETURN_OK);
                $this->security_response->setMessage(SecurityResponse::CATEGORY_RETURN_OK_MESSAGE);
            }
        }

        $this->security_response->setData($data);
        return $this->response->setContent($this->security_response);
    }

    /**
     * @param $name
     * @return Response
     */
    public function addCategoryAction($name)
    {
        $data = [];

        //创建一个分类
        $category = $this->get('api.user')->addCategory($name);

        if($category == null) {
            $this->security_response->setMessage(SecurityResponse::CATEGORY_HAS_EXISTS_MESSAGE);
            $this->security_response->setStatus(SecurityResponse::CATEGORY_HAS_EXISTS);
        } else {
            $this->security_response->setMessage(SecurityResponse::CATEGORY_CREATE_OK_MESSAGE);
            $this->security_response->setStatus(SecurityResponse::CATEGORY_CREATE_OK);

            $data['name']   = $category->getName();
            $data['id']     = $category->getId();
        }

        $this->security_response->setData($data);
        return $this->response->setContent($this->security_response);

    }

    /**
     * @param $name
     * @return Response
     */
    public function updateCategoryAction($name, $new_name)
    {
        $data = [];

        $category = $this->get('api.user')->updateCategory($name, $new_name);

        if($category == null) {
            $this->security_response->setMessage(SecurityResponse::CATEGORY_NOT_EXISTS_MESSAGE);
            $this->security_response->setStatus(SecurityResponse::CATEGORY_NOI_EXISTS);
        } else {
            $this->security_response->setMessage(SecurityResponse::CATEGORY_HAS_UPDATED_MESSAGE);
            $this->security_response->setStatus(SecurityResponse::CATEGORY_HAS_UPDATED);

            $data['name']   = $category->getName();
            $data['id']     = $category->getId();
        }

        $this->security_response->setData($data);
        return $this->response->setContent($this->security_response);
    }

    public function addBillAction()
    {
        $data = [];

        /**
         * 所有业务逻辑由service来做，此处只做任务调度和结果处理
         */
        $bill = $this->get('api.user')->addBill();

        if($bill == null) {
            $this->security_response->setStatus(SecurityResponse::BILL_ADD_FAILURE);
            $this->security_response->setMessage(SecurityResponse::BILL_ADD_FAILURE_MESSAGE);
        } else {
            $this->security_response->setStatus(SecurityResponse::BILL_ADD_SUCCESS);
            $this->security_response->setMessage(SecurityResponse::BILL_ADD_SUCCESS_MESSAGE);

            $data['id']         = $bill->getId();
            $data['price']      = $bill->getPrice();
            $data['what_for']   = $bill->getWhatFor();
            $data['time']       = $bill->getTime();
            $data['location']   = $bill->getLocation();
            $data['address']    = $bill->getAddress();
            $data['type']       = array(
                'name'  =>  $bill->getType()->getName(),
                'id'    =>  $bill->getType()->getId()
            );
            $data['category']   = array(
                'name'  =>  $bill->getCategory()->getName(),
                'id'    =>  $bill->getCategory()->getId()
            );
        }

        $this->security_response->setData($data);
        return $this->response->setContent($this->security_response);

    }

    /**
     * @param $page
     * @param $limit
     * @return Response
     * 获取用户账单，具体实现可看
     * ApiBundle\Service\UserService。
     * 此处只是调度服务并返回数据，并无实质业务流程操作
     */
    public function getBillsAction($page, $limit)
    {
        $data = $this->get('api.user')->getBills($page, $limit);

        $this->security_response->setData($data);
        $this->security_response->setMessage(SecurityResponse::BILL_RETURN_OK_MESSAGE);
        $this->security_response->setStatus(SecurityResponse::BILL_RETURN_OK);

        return $this->response->setContent($this->security_response);
    }

}
